import 'dart:io';

import 'package:bwt_ad_flutter/ad/widget/BwtNativeExpressAdWidget.dart';
import 'package:bwt_ad_flutter_example/util/Log.dart';
import 'package:bwt_ad_flutter_example/widget/SimpleWidget.dart';
import 'package:flutter/material.dart';
import 'package:bwt_ad_flutter/ad/loader/BwtNativeExpressAdLoader.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../util/DisplayUtil.dart';

class NativeExpressAdPage extends StatefulWidget {
  const NativeExpressAdPage({super.key});

  @override
  State<StatefulWidget> createState() => _NativeExpressAdPageState();
}

class _NativeExpressAdPageState extends State<NativeExpressAdPage> {
  final BwtNativeExpressAdLoader _nativeExpressAdLoader =
      BwtNativeExpressAdLoader();
  bool _loading = false;
  bool _visible = true;

  BwtNativeExpressAdWidget? _nativeExpressAd;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("信息流模板广告"),
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SimpleWidget(
              width: 200,
              height: 44,
              color: Colors.blue,
              alignment: Alignment.center,
              onTap: () {
                _loadAd();
              },
              child: const Text(
                '加载广告',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),

            /// 信息流模板广告
            Visibility(
              visible: _visible,
              child: Container(
                width: double.infinity,
                margin: const EdgeInsets.only(top: 24),
                child: _nativeExpressAd,
              ),
            ),

            SimpleWidget(
              width: 180,
              height: 45,
              color: Colors.blue,
              margin: const EdgeInsets.only(top: 16),
              alignment: Alignment.center,
              child: const Text(
                '改变可见性',
                style: TextStyle(color: Colors.white, fontSize: 14),
              ),
              onTap: () {
                setState(() {
                  _visible = !_visible;
                });
              },
            )
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();

    // 设置广告加载成功监听
    _nativeExpressAdLoader.onAdLoad = (bwtAd) {
      setState(() {
        _nativeExpressAd = bwtAd.expressAd;
      });
      _loading = false;
      Log.d('信息流模板广告加载成功...');
      Fluttertoast.showToast(msg: "广告加载成功");
    };

    // 设置广告加载失败监听
    _nativeExpressAdLoader.onAdFailed = (msg) {
      _loading = false;
      Log.d('信息流模板广告加载失败, $msg');
      Fluttertoast.showToast(msg: "广告加载失败");
    };

    // 设置广告展示监听
    _nativeExpressAdLoader.onAdExpose = (bwtAd) {
      Log.d('信息流模板广告展示成功...');
    };

    // 设置广告被点击监听
    _nativeExpressAdLoader.onAdClick = (bwtAd) {
      Log.d('信息流模板广告被点击...');
    };

    // 设置广告关闭监听
    _nativeExpressAdLoader.onAdClose = (bwtAd) {
      Log.d('信息流模板广告被关闭...');
    };
  }

  @override
  void dispose() {
    /// 释放广告
    _nativeExpressAdLoader.release();
    super.dispose();
  }

  /// 加载广告
  void _loadAd() {
    if (_loading) return;
    _loading = true;
    _nativeExpressAdLoader.loadAd(
      // 广告位ID，不同端的广告位ID可能不一致，需替换成自己相应端的广告位ID
      posId: Platform.isAndroid ? '64967792' : '87522319',
      // 广告宽度
      adWidth: DisplayUtil.screenWidth,
    );
  }
}
